package com.shmet.tool.localcall.frame.strategy;

import com.shmet.tool.localcall.frame.service.BizService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author shmet
 * @date 2022/7/20 15:48
 * @desc 注入上下文，取指定接口的所有实例
 *  本示例中未使用
 */
@Component
public class BizStrategyFactory implements ApplicationContextAware {

    private List<BizService> instanceList;
    private Map<String, BizService> instanceMap;

    public Object getServiceBean(String name) {
        return this.instanceMap.get(name);
    }

    /**
     * 取所有示例
     *
     * @Author: shmet
     * @Date: 2022/7/19 15:19
     * @return: java.util.List<com.weaver.common.form.real.strategy.FdRealDetailService>
     */
    public List<BizService> listInstance() {
        return instanceList;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.instanceMap = applicationContext.getBeansOfType(BizService.class);
        this.instanceList = new ArrayList<>(instanceMap.values());
    }
}